package client;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import game.Game;
import game.Player;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout.Alignment;

/**
 * JPanel pour le jeu
 * @author Julien Roch
 * @version 0.1
 */
public class JPanelGame extends JPanel{
	
	/**
	 * pour la serialization
	 */
	private static final long serialVersionUID = 6475242734158233398L;
	private JPanel objectsList;
	private JPanelGrid grid;
	private JspPlayersInfos playersList;	
	private JPanelMagicObjects objects;
	/**
	 * L'application client
	 */
	private ClientApplication clientApp;
	
	// Tailles
	private static final int OBJECTS_LIST_WIDTH = 100;
	private static final int PLAYERS_LIST_WIDTH = 150;
	
	/**
	 * Constructuer
	 * @param app L'application client
	 * @param game Le jeu
 	 * @param players La liste des joueurs
	 */
	public JPanelGame(ClientApplication app, Game game, Player[] players){
		this.clientApp = app;
		
		// on initialise les panels
		this.objectsList = new JPanel();
		this.grid = new JPanelGrid(game,app);
		game.getPlayers();
		this.playersList = new JspPlayersInfos(players, true);
		playersList.setHorizontalScrollBar(null);
		playersList.showPlayerInGame(app.internals.getNextPlayer());

		// Ajout
		JPanel p = new JPanel();
		p.add(this.grid);
		JScrollPane jsp = new JScrollPane(p);
		jsp.setPreferredSize(new Dimension( 300,300)); // A definir
		
		
		// Creation du bouton "Terminer la partie" pour le proprietaires..
		if(game.getCreator().equals(app.internals.userName)){
			JButton bntEndGame = new JButton("Terminer");
			bntEndGame.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {
					JPanelGame.this.clientApp.internals.stopGame();
				}
			});
			this.objectsList.add(bntEndGame);
		}
		
		GroupLayout layout = new GroupLayout(this);
		// construction des grilles
		this.setLayout(layout); // objets, grille et liste des joueurs
		layout.setHorizontalGroup(
				layout.createParallelGroup(Alignment.LEADING).addGroup(
						(layout.createSequentialGroup()
								.addComponent(this.objectsList,OBJECTS_LIST_WIDTH,OBJECTS_LIST_WIDTH,OBJECTS_LIST_WIDTH)
								.addComponent(jsp,GroupLayout.DEFAULT_SIZE,300,Short.MAX_VALUE)
								.addComponent(this.playersList,PLAYERS_LIST_WIDTH,PLAYERS_LIST_WIDTH,PLAYERS_LIST_WIDTH))
						)
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(Alignment.LEADING).addGroup(
						(layout.createParallelGroup()
								.addComponent(this.objectsList,OBJECTS_LIST_WIDTH,OBJECTS_LIST_WIDTH,OBJECTS_LIST_WIDTH)
								.addComponent(jsp,GroupLayout.DEFAULT_SIZE,300,Short.MAX_VALUE)
								.addComponent(this.playersList,PLAYERS_LIST_WIDTH,PLAYERS_LIST_WIDTH,PLAYERS_LIST_WIDTH))
						)
				);
		
		this.objects = new JPanelMagicObjects(clientApp.internals.getMagicObjects(), clientApp.internals);
		this.objectsList.add(this.objects);
		
		
		
		// on rajoute les panels
		this.add(this.objectsList);
		this.add(jsp);
		this.add(this.playersList);
		
	}
	
	/**
	 * Permet de fixer le joueur courant
	 * @param nextPlayer Le joueur
	 */
	public void setCurrentPlayer(Player nextPlayer) {
		this.playersList.showPlayerInGame(nextPlayer);
	}
	
	/**
	 * Permet de rafraichir les scores
	 * @param players lq liste des joueurs
	 * @param nextPlayer le joueur suivant
	 */
	public void refreshPlayersScores(Player[] players, Player nextPlayer) {
		this.playersList.refreshGames(players, true, nextPlayer);
	}
	/**
	 * Obtient la grille
	 * @return la grille
	 */
	public JPanelGrid getGrid(){
		return this.grid;
	}

	/**
	 * @return the objects
	 */
	public JPanelMagicObjects getObjects() {
		return objects;
	}
	
}
